package gms;

import java.util.Random;

import gms.entities.um.Role;
import gms.entities.um.RolePermission;
import gms.entities.um.User;
import gms.service.UmUtil;
import gms.service.UserManagementService;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Administrator
 */
@Transactional
public class testUM {

	private ApplicationContext context;
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;

	@Before
	public void begin() {
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}

	@After
	public void end() {
		transaction.commit();
	}

	@Test
	public void test() {
		System.out.println(context.getBean("sessionFactory"));
		System.out.println(context.getBean("transactionManager"));
	}

//	@Test
//	public void testYHGL() {
//		UMDaoImpl dao =  context.getBean(UMDaoImpl.class);
//		dao.getHibernateTemplate().save(new User("aa","bb"));
//	}

	@Test
	public void testYHGL1() {
		

		RolePermission rp1 = new RolePermission("管理用户");
		RolePermission rp2 = new RolePermission("管理器材");
		RolePermission rp3 = new RolePermission("管理赛事");
		RolePermission rp4 = new RolePermission("管理场地");
		session.save(rp1);
		session.save(rp2);
		session.save(rp3);
		session.save(rp4);
		
		
		Role role2 = new Role("超级管理员");
		role2.getRps().add(rp1);
		role2.getRps().add(rp2);
		role2.getRps().add(rp3);
		role2.getRps().add(rp4);
		session.save(role2);
		
		
		Role role3 = new Role("器材管理员");
		role3.getRps().add(rp2);
		session.save(role3);
		
		Role role4 = new Role("场地管理员");
		role4.getRps().add(rp4);
		session.save(role4);
		
		Role role5 = new Role("赛事管理员");
		role5.getRps().add(rp3);
		session.save(role5);
		
		
		
//		Role role = new Role("��ͨ�û�");
//		session.save(role);
		
//		role.getRps().add(rp1);
//		role.getRps().add(rp2);
//		role.getRps().add(rp3);
//		role.getRps().add(rp4);
//		session.save(role);
//
//		User user = new User();
//		user.setUserName("cck");
//		user.setUserPassword("cck");
//		user.setAddress("�㶫����");
//		user.setEmail("mainem@163.com");
//		user.setIdCard("123456");
//		user.setRealName("ck");
//		user.setPhone("13515");
//		user.setRole(role);
//		session.save(user);
//		
//		user = new User();
//		user.setUserName("lrx");
//		user.setUserPassword("lrx");
//		user.setAddress("����");
//		user.setEmail("3232@163.com");
//		user.setIdCard("12156");
//		user.setRealName("rx");
//		user.setPhone("33333515");
//		user.setRole(role);
//		session.save(user);

	}

	@Test
	public void testYHGL2() {
		User user = (User) session.get(User.class, 1);
		user.setRole(null);
		session.update(user);

		Role role = (Role) session.get(Role.class, 1);
		session.delete(role);
	}

	@Test
	public void testYHGL3() {
		User user = (User) session.get(User.class, 1);
		user.setRole(null);
		session.update(user);

		Role role = (Role) session.get(Role.class, 1);
		session.delete(role);
	}

	@Test
	public void testYHGL4() {
		// UserManagementDao dao = context.getBean(UserManagementDao.class);
		// System.out.println(dao.getUser("cck", "cck"));
		// System.out.println(dao.getUser("cck", "1"));

		UserManagementService service = context
				.getBean(UserManagementService.class);
		
		System.out.println(service.getUser(new User("cck", "cck")));
		System.out.println(service.getUser(new User("cck", "cck1")));

	}
	
	@Test
	public void testYHGL5() {
		UserManagementService service = context
				.getBean(UserManagementService.class);
		System.out.println("++++++++++++=="+service.registeredCheck("cck"));
		System.out.println("++++++++++++=="+service.registeredCheck("ccka"));
	}
	
	@Test
	public void testYHGL6() {
		UserManagementService service = context
				.getBean(UserManagementService.class);
		User user = service.getUser(1);
		System.out.println(user);
//		user.setPhone("32342");
//		service.updateUser(user);
	}
	
	//��֤ģ����ѯ
	@Test
	public void testYHGL7() {
		UserManagementService service = context
				.getBean(UserManagementService.class);
		User user = new User();
//		user.setUserId(1);
		user.setUserName("cc");
		System.out.println(service.queryUser(UmUtil.transUser(user)));
		
	}
	@Test
	public void testYHGL8() {
		UserManagementService service = context
				.getBean(UserManagementService.class);
		service.deleteUser(2);
		User user = service.getUser(2);
		System.out.println(user);
	}
	
	@Test
	public void testYHGL9() {
		UserManagementService service = context
				.getBean(UserManagementService.class);
		
		User user = new User();
//		user.setUserPassword("x");;
		user = UmUtil.transUser(user);
		user.setUserId(4);
		System.out.println(user);
		System.out.println(service.getQueryNum(user));
		
//		System.out.println(service.getUserWithPage(user, 1, 5));
		
//		Role role = new Role();
//		role.setRoleId(5);
//		User user = service.getUser(3);
//		user.setRole(role);
//		
//		service.updateUser(user);
//		
//		User user2 = service.getUser(3);
//		System.out.println(user2.getRole().getRps());
		
//		service.deleteRole(4);
		
//		Integer rp [] = {4};
//		service.modifiRole("���ع���Ա", rp);
		
//		String roleName = "����";
//		Integer rp [] = {1,2,3,4};
//		service.addRole(roleName, rp);WW
//		System.out.println(service.getAllRole());
		
//		System.out.println(service.getRole(1));
		
//		List<RolePermission> rps = service.getAllPermissions();
//		
//		Role role = new Role();
//		role.setRoleName("���Ĺ���Ա");
//		role.getRps().add(rps.get(1));
//		role.getRps().add(rps.get(3));
//		
//		session.save(role);
	}
	
	
	@Test
	public void testYHGL10() {
		UserManagementService service = context
				.getBean(UserManagementService.class);
		String x[] = {"��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","֣","л","��","��","��","��","��","��","��","��","��","��","��","��","��","��","Ԭ","��","��","��","��","��","��","Ҷ","��","��","κ","��","��","��","��","Ҧ","¬","��","��","��","̷","½","��","��","��","ʯ","��","��","��","Τ","��","��","��","��","��","��","��","��","��","��","Ѧ","��","��","��","��","��","ʷ","��","��","��","��","ë","��","��","��","��","Ǯ","��","��","��","��","Ī","��","��","��"};
		String m[] = {"��","��","Ϊ","��","��","��","˵","��","��","��","��","��","��","��","Ҫ","��","Ҳ","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","ʮ","ʵ","��","��","��","ȫ","��","��","��","��","��","��","��","ÿ","��","��","��","��","̫","��","��","��","��","��","��","��","��","ˮ","��","��","��","��","ȴ","��","��","��","��","��","��","��","��","��","ľ","��","��","��","��","��","˯","չ","��","��","��","��","��","��","Ⱥ","��","��","��","ͥ","��","��","��","��","л","��","��","��","��","��","��","��","ֹ","��","��","��","��","��","��","��","��","��","��","��","æ","��","��","��","��","��","��","��","��","��","��","��","��","��","Χ","��","��","��","��","��","��","��","ѩ","��","��","��","��","��","��","��","��","ֵ","��","��","��","��","��","��","��","ɳ","��","��","��","��","��","��","��","��","��","��","��","��","��","ʥ","��","��","��","��","¶","԰","ţ","��","��","��","��","��","��","��","��","Ī","��","��","��","��","Ȩ","³","��","̬","��","Ʊ","��","Ѱ","ɱ","��","ʤ","��","��","��","��","��","��","��","��","��","��","¥","��","��","��","��","׷","��","ְ","��","��","��","¼","˿","��","��","��","��","��","��","��","��","Ҷ","��","��","��","��","��","��","��","��","��","��","��","��","��","Ϸ","��","��","ҩ","��","��","��","��","��","��","��","ɭ","��","��","��","��","��","��","��","ɢ","��","��","��","��","��","��","��","Ͷ","��","��","��","��","��","ѹ","��","��","��","��","��","ϴ","��","��","��","��","��","��","��","״","��","��","ϯ","��","ͯ","��","��","��","��","ũ","��","��","Ұ","��","��","��","��","��","��","��","��","��","?","��","��","��","��","��","��","ķ","��","��","��","��","��","��","��","��","��","��","ȱ","��","��","ŷ","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","ί","ӵ","��","��","̽","ѽ","Ӫ","��","ҡ","Ū","��","��","ŵ","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","��","Ь","��","ģ","��","��","��","��","��","��","��","ǹ","��","��","��","��","��","��","?","��","��","��","̹","��","��","˳","��","��","��","��","��","��","��","��","��","Ԥ","��","��","��","?","��","��","��","��","��","ʢ","��","��","��","��","��","��","��","��","��","��"};
		Random random = new Random();
		
		for(int i = 0 ; i<100 ; i++){
			User user = new User();
			user.setUserName(x[random.nextInt(x.length-1)] + m[random.nextInt(m.length-1)]);
			user.setUserPassword(""+i);
			service.saveUser(UmUtil.transUser(user));
		}
		
		
	}
	
}
